package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import server.InvalidLoginException;
import controller.controllerFunc;

/**
 * Class is responsible for login frame.
 * Frame contains: 	fields of connection data - user name, password, and server ip
 * 					Login button enable logging into fw system manager after certifying 
 * 					connection data. 
 * 					Help button that open the help frame. 
 */
public class LoginFrame extends JFrame
{
	/**
	 * Serial version ID - for serialization
	 */
	private static final long serialVersionUID = 2478538124878851676L;
	
	private		JSplitPane		splitPaneV;
	private		JPanel			loginFields;
	private		JPanel			panelFields;
	private		JPanel		 	panelActions;
	private   	JTextField      txtUserName;
	private 	JPasswordField  txtPassword;
	private 	JTextField      txtIp;
	private 	HelpFrame  		helpWin;

	/**
	 * C'tor - 
	 * Define the frame.
	 */
	public LoginFrame()
	{
		/* set the frame title */
		setTitle( "FW Login" );
		JPanel topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

		// Create the panels
		createPanelFields();
		createPaneActions();

		// Create a splitter pane
		splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
		topPanel.add( splitPaneV, BorderLayout.CENTER );
		splitPaneV.setLeftComponent( panelFields);
		splitPaneV.setRightComponent(panelActions);
	
		pack();
		setSize(new Dimension(300, 200));
		setLocationRelativeTo(null);
		setEnabled(true);
		setVisible(true);
	}

	/**
	 * Create the panel of the fields  (the upper panel)
	 * @param  N\A
	 * @return N\A
	 */
	private void createPanelFields()
	{
		/* Create all the fields and thier label's */
		txtUserName = new JTextField();
		txtPassword = new JPasswordField();
		txtIp       = new JTextField();
		JLabel lblUserName = new JLabel("User Name: ");
		JLabel lblPassword = new JLabel("Password: " );
		JLabel lblIp = new JLabel("Server IP: " );

		/* put all the fields on the panel */ 
		loginFields = new JPanel();
		loginFields.setLayout(new BoxLayout(loginFields, BoxLayout.X_AXIS));
		loginFields.add(lblUserName);
		loginFields.add(txtUserName);
		loginFields.setLayout(new BoxLayout(loginFields, BoxLayout.Y_AXIS));
		loginFields.add(lblPassword);
		loginFields.add(txtPassword);
		panelFields = new JPanel();
		panelFields.setLayout(new BoxLayout(panelFields, BoxLayout.X_AXIS));
		panelFields.add(loginFields);
		panelFields.setLayout(new BoxLayout(panelFields, BoxLayout.Y_AXIS));
		panelFields.add(lblIp);
		panelFields.add(txtIp);
	}

	/**
	 * Create the panel of the operations (the lower panel)
	 * @param  N\A
	 * @return N\A
	 */
	private void createPaneActions()
	{
		/*Creaet the panel's buttons */
		JButton loginButton = new JButton( "Login" );
		JButton helpButton = new JButton( "Help" );

		panelActions = new JPanel();
		panelActions.setLayout(new BoxLayout(panelActions, BoxLayout.X_AXIS));
		panelActions.add(loginButton);
		panelActions.add(helpButton);
		
		/* create action listener for login button - 
		 * click on this button will certify the connection details and 
		 * perform logging into the FW system manager*/
		loginButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ae) 
		    {
				LoginFW();
			}
		}
		);	
		
		/* create action listener for help button - 
		 * click on this button will open the help frame */
		helpButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
		    {
				ShowHelp(Globals.LOGIN);
			}
		}
		);	
	}

	/**
	 * Load the help data of the topic that choosen
	 * click on this button will certify the connection details and 
	 * perform logging into the FW system manager
	 * 
	 * @param  N\A
	 * @return N\A
	 */
	public void LoginFW()
	{
		// case no data insert into user or password fields
		if(txtUserName.getText().equals("") )
		{
			JOptionPane.showMessageDialog(this,
			"You must enter User Name before Login");
			return;
		}			
		else 
		{
			try
			{
				/* send the connection details that the user insert for certifying */
				//server.FirewallSession firewall = controllerFunc.PerformLogin( txtIp.getText(), txtUserName.getText(), txtPassword.getText());
				server.FirewallSession firewall = controllerFunc.PerformLogin( txtIp.getText(), txtUserName.getText(), new String(txtPassword.getPassword()));
				
				/* The connection details cetifying successfully - open the fw systm manager */ 
				controller.FirewallSession ui = new controller.FirewallSession ();

				/* Initial the fw system manager */ 
				ui.set_firewall(firewall);
				new Initialization(ui); 
				dispose();
			}
			/* The connection details cetifying faild  becouse of invalid username/password - show information message to the user*/
			catch (InvalidLoginException e )    
			{ 
				JOptionPane.showMessageDialog(this, "Invalid username/password!\nYou are not authorized to perform login");
			}
			/* The connection details cetifying faild  becouse of client exception - show information message to the user*/
			catch (Exception e) 
			{
				JOptionPane.showMessageDialog(this, "Client exception: " + e.toString());
				e.printStackTrace();
			}
		}
	}	

	/**
	 * Open the help frame
	 * @param  String hlpTopic - the name of the topic to show in the help frame
	 * @return N\A
	 */
	public void ShowHelp(String hlpTopic)
	{
		if(helpWin == null)
		{
			helpWin = new HelpFrame();
		}				
		
		helpWin.ShowDesc(hlpTopic);
	}
}	
